package com.example.administrator.shzs.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.administrator.shzs.R;
import com.example.administrator.shzs.entity.ShopSortData.ShopDatalistBean;

import java.util.List;

/**
 * 分类适配器
 */
public class ShopSortAdapter extends BaseAdapter {


    int mSelect = 0;   //选中项

    private Context context;
    private List<ShopDatalistBean> sortDatas;

    public ShopSortAdapter(Context context, List<ShopDatalistBean> lists) {
        this.context = context;
        this.sortDatas = lists;
    }

    @Override
    public int getCount() {
        return sortDatas == null ? 0 : sortDatas.size();
    }

    @Override
    public ShopDatalistBean getItem(int position) {
        return sortDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {

            convertView = View.inflate(context, R.layout.shop_sort_item, null);
            holder = new ViewHolder();

            holder.tvSortName = (TextView) convertView.findViewById(R.id.tv_sortName_sortitem);
            convertView.setTag(holder);
        }

        holder = (ViewHolder) convertView.getTag();

        ShopDatalistBean data = getItem(position);

        holder.tvSortName.setText(data.getSortName());


        //Item背景颜色
        if (mSelect == position) {

            holder.tvSortName.setBackgroundResource(R.color.colorWhite);
        } else {
            holder.tvSortName.setBackgroundResource(R.color.listview);
        }

        return convertView;
    }

    /**
     * Item被选中时切换背景颜色
     *
     * @param positon
     */
    public void changeSelected(int positon) { //
        if (positon != mSelect) {
            mSelect = positon;
            notifyDataSetChanged();//刷新
        }
    }


    class ViewHolder {
        TextView tvSortName;
    }
}
